package com.erytech.linktalk.web.client.dataview;

import com.erytech.linktalk.web.client.datasource.UserDataSource;
import com.erytech.linktalk.web.client.datasource.UserUpdateEvent;
import com.erytech.linktalk.web.client.framework.DataUpdateEvent;
import com.erytech.linktalk.web.client.framework.DataView;
import com.erytech.linktalk.web.client.framework.MouseAdaptor;
import com.erytech.linktalk.web.client.service.UserInfo;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserListView extends DataView {

	DockPanel container = new DockPanel();

	FlexTable usersTable = new FlexTable();

	ScrollPanel scroller = new ScrollPanel(usersTable);

	UserInfoPopup userInfoPopup = new UserInfoPopup();

	public UserListView(UserDataSource ds) {
		super(ds);

		container.setSize("100%", "100%");

		container.add(scroller, DockPanel.CENTER);
		container.setCellHeight(scroller, "100%");
		container.setCellWidth(scroller, "100%");

		scroller.setSize("100%", "100%");
		scroller.setStyleName("user-panel");

		usersTable.setWidth("100%");

		initWidget(container);
	}

	public void onWindowResized() {
		scroller.setHeight((container.getOffsetHeight()) + "px");
	}

	static class UserInfoPopup extends PopupPanel {

		FlexTable table = new FlexTable();

		public UserInfoPopup() {
			super(true);
			setStyleName("userInfo-PopupPanel");
			Label genderLabel = new Label("性别: ");
			genderLabel.setStyleName("userInfo-PopupName");
			table.setWidget(0, 0, genderLabel);
			table.setText(0, 1, "");
			Label descrLabel = new Label("个人介绍:");
			descrLabel.setStyleName("userInfo-PopupName");
			table.setWidget(1, 0, descrLabel);
			table.setText(1, 1, "");
			table.getColumnFormatter().setWidth(0, "80px");
			table.getColumnFormatter().setWidth(1, "120px");
			add(table);
		}

		public void showUserInfo(UserInfo userInfo, HTML link) {
			// update info
			setTitle(userInfo.getName());
			table.setText(0, 1, ((userInfo.getSex() == UserInfo.FEMALE) ? "女"
					: "男"));
			table.setText(1, 1, userInfo.getDescription());
			// show
			int left = link.getAbsoluteLeft() + 8;
			int top = link.getAbsoluteTop() + 32;
			setPopupPosition(left, top);
			show();
		}
	}

	public void showWaitIcon() {
		usersTable.setWidget(0, 0, new Image("wait.gif"));
	}

	class UserView extends Composite implements ClickListener {

		HTML link;

		UserInfo userInfo;

		public UserView(final UserInfo userInfo) {
			this.userInfo = userInfo;

			HorizontalPanel outerPanel = new HorizontalPanel();
			outerPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
			outerPanel.setWidth("100%");
			outerPanel
					.setStyleName(userInfo.getSex() == UserInfo.FEMALE ? "user-female"
							: "user-male");

			HorizontalPanel innerPanel = new HorizontalPanel();
			innerPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
			innerPanel.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);

			String displayUserName = userInfo.getName();
			if (userInfo == ((UserDataSource) getDataSource()).getUserInfo()) {
				displayUserName = "*" + displayUserName;
			}
			link = new HTML("[<a href='javascript:;'>" + displayUserName
					+ "</a>]: ");
			link.addClickListener(this);
			link.addMouseListener(new MouseAdaptor() {
				public void onMouseEnter(Widget widget) {
					userInfoPopup.showUserInfo(userInfo, link);
				}

				public void onMouseLeave(Widget sender) {
					userInfoPopup.hide();
				}
			});

			innerPanel.add(getPortrait(userInfo.getImage()));
			innerPanel.add(link);
			innerPanel.add(new Label(userInfo.getDescription()));

			outerPanel.add(innerPanel);
			initWidget(outerPanel);
		}

		private Image getPortrait(String index) {
			UserDataSource userDS = (UserDataSource) getDataSource();
			String imgSrc = userDS.getRoomDataSource().getPortraitDataSource()
					.getPortraitSrc(index);
			return new Image(imgSrc);
		}

		public void onClick(Widget sender) {
			((UserDataSource) getDataSource()).saidTo(userInfo.getName());
		}
	}

	public void onDataUpdateEvent(DataUpdateEvent event) {
		if (event instanceof UserUpdateEvent) {
			UserUpdateEvent userUpdateEvent = (UserUpdateEvent) event;
			if (userUpdateEvent.getType() == UserUpdateEvent.POLL_SUCCESS) {
				UserInfo[] users = userUpdateEvent.getUsers();
				if (users == null) return;
				usersTable.clear();
				usersTable.setWidget(0, 0, new UserView(
						((UserDataSource) getDataSource()).getUserInfo()));
				for (int i = 0; i < users.length; i++) {
					usersTable.setWidget(i + 1, 0, new UserView(users[i]));
				}
			} else if (userUpdateEvent.getType() == UserUpdateEvent.POLL_FAILURE) {
				usersTable.clear();
				usersTable.setText(0, 0, ((UserUpdateEvent) event)
						.getThrowable().getMessage());
			} else {}
		} else {}
	}

}
